package lab03.text14;

import java.sql.*;

public class Text02 {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        Connection connection = null;
        Statement statement = null;

        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/java_2017?serverTimezone=UTC", "root", "193749");
            statement = connection.createStatement();

            //创建基本表
            statement.execute("CREATE TABLE userinfo" + " (usercode VARCHAR (4) not NULL," + "username VARCHAR (10)," + "password VARCHAR (6)," + "email VARCHAR (40)," + "PRIMARY KEY (usercode))");

            //向数据库中插入数据
            statement.executeUpdate("INSERT INTO userinfo(usercode,username,password,email) VALUES ('7101','宋晓梅','123456','xiaomei@yahoo.com.cn');");
            statement.executeUpdate("INSERT INTO userinfo(usercode,username,password,email) VALUES ('7102','关松杰','654321','songjie@263.net');");
            statement.executeUpdate("INSERT INTO userinfo(usercode,username,password,email) VALUES ('7103','李晓梅','123654','xiaomei@yahoo.com.cn ');");
            statement.executeUpdate("INSERT INTO userinfo(usercode,username,password,email) VALUES ('7104','张松岩','456123','songyan@yahoo.com.cn ');");
            statement.executeUpdate("INSERT INTO userinfo(usercode,username,password,email) VALUES ('7105','王亚杰','321456','yajie@hotmail.com ');");
            statement.executeUpdate("INSERT INTO userinfo(usercode,username,password,email) VALUES ('7106','曲小声','654321','xiaosheng@263.net');");

            //输出用户数据
            ResultSet resultSet = statement.executeQuery("SELECT * FROM userinfo ");
            while (resultSet.next() == true) {
                System.out.print(resultSet.getString(1) + "  ");
                System.out.print(resultSet.getString(2) + "  ");
                System.out.print(resultSet.getString(3) + "  ");
                System.out.println(resultSet.getString(4));
            }

            //插入记录
            System.out.println("更新数据：");
            statement.executeUpdate("INSERT INTO userinfo(usercode,username,password,email) VALUES ('7107','张三','633321','zhangsan@263.net');");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}
